package main

import (
	"fmt"
	"github.com/zhan3333/glog"
	"os"
	"time"

	"server/src/zyplog"
)

var g_log *zyplog.ZypLoger

// 初始化全局变量
func initGlobal() {
	if len(os.Args) > 1 {
		if os.Args[1] == "-v" {
			fmt.Println(VER)
			os.Exit(0)
		}
	}
}

func createLoger(lt int, path string) (*zyplog.ZypLoger, error) {
	//创建日志
	var logType zyplog.LogType
	switch lt {
	case 0:
		logType = zyplog.LOG_STDOUT
	case 1:
		logType = zyplog.LOG_FILE
	case 2:
		logType = zyplog.LOG_SYSLOG
	}
	loger, err := zyplog.NewZypLoger(logType, path)
	if err != nil {
		fmt.Println("创建日志失败：", err.Error())
		return nil, err
	}
	return loger, nil
}

func zLog(args ...interface{}) {
	glog.Channel("gpsprocessor2d").Infoln(args)
}

func zLogError(args ...interface{}) {
	glog.Channel("gpsprocessor2d").Errorln(args...)
}

func intTimeToTime(dt int) time.Time {
	d := dt / 1000000
	t := dt % 1000000

	year := d / 10000
	month := d % 10000 / 100
	day := d % 100
	hour := t / 10000
	minute := t % 10000 / 100
	second := t % 100

	ret := time.Date(year, time.Month(month), day, hour, minute, second, 0, time.Now().Location())
	return ret
}

func intTimeToDuration(timeBegin, timeEnd int) int {
	if timeBegin == 0 || timeEnd == 0 {
		return 0
	}

	dt1 := intTimeToTime(timeBegin)
	dt2 := intTimeToTime(timeEnd)

	dur := dt2.Sub(dt1)
	return int(dur.Seconds())
}

func timeStampToString(t int64, format string) string {
	if format == "" {
		format = "2006-01-02 15:04:05"
	}

	tt := time.Unix(t, 0)
	return tt.Format(format)
}
